﻿
using Models.Common;
using Models.Device;
using Models.TaskPlain;
using NetTaste;
using Newtonsoft.Json;
using UtilsORM;
using UtilsORM.MySqlData;
using static Models.Common.EnumDeviceState;
using UtilsBase;
using GatewayServerService.CloudCenter;

namespace GatewayServerService.Device
{
    /// <summary>
    /// //普锐森社-温湿光照co2传感器
    /// </summary>
    public class ServiceDeviceRS4851023_GWSCO2
    {
        ServiceCloudDeviceValueLog log = new ServiceCloudDeviceValueLog();
        //回调获获状态,为了避免误差，查状态和执行，都返回了 当前开关状态
        public void callBackUpdate(MQReqModel<object> httpReqModel)
        {
            try
            {
                ModelReceverData modelDeviceData = httpReqModel.toServerData;
                CommandLogModel commandLogModel = httpReqModel.toGatewayData;
                //ModelCommandAgreement modelCommandAgreement = JsonConvert.DeserializeObject<ModelCommandAgreement>(commandLogModel.parameter);
                //ModelCommandAgreementParameter modelCommandAgreementParameter = UtilsObject.DynamicToEntity<ModelCommandAgreementParameter>(modelCommandAgreement.agreementParameter);
                //List<ModelCommandAgreementParameterCommand> commands = UtilsObject.DynamicToEntity<List<ModelCommandAgreementParameterCommand>>(modelCommandAgreement.agreementParameter.command);
                //ModelCommandAgreementParameterCommand command = commands.Where(c => c.key == commandLogModel.key).FirstOrDefault();
                string result = modelDeviceData.commandResults[0].value;
                if (string.IsNullOrWhiteSpace(result))
                {
                    return;
                }

                if (commandLogModel.key == "light")
                {
                    //01 03 02 00 4A 39 B3
                    string dataHex1 = result.Substring(9, 5);
                    int light = new UtilsDataTypeChange().hex16Convert10(dataHex1);

                    log.writeSensorlog(commandLogModel.d_guid, "light", light.ToString(), "光照度", result);
                }
                else if (commandLogModel.key == "co2")
                {
                    //01 03 02 02 63 F9 0D
                    string dataHex1 = result.Substring(9, 5);
                    int co2 = new UtilsDataTypeChange().hex16Convert10(dataHex1);
                    log.writeSensorlog(commandLogModel.d_guid, "co2", co2.ToString(), "二氧化碳", result);
                }
                else if (commandLogModel.key == "temperatureANDhumidity")
                {
                    //  01 03 04 02 1E 00 FF DB CD
                    string dataHex1 = result.Substring(9, 5);
                    double humidity = new UtilsDataTypeChange().hex16Convert10(dataHex1) * 0.1;

                    string dataHex2 = result.Substring(15, 5);
                    double temperature = new UtilsDataTypeChange().hex16Convert10(dataHex2) * 0.1;
                    log.writeSensorlog(commandLogModel.d_guid, "humidity", UtilsNumber.GetNumberForLength(humidity, 2).ToString(), "湿度", result);
                    log.writeSensorlog(commandLogModel.d_guid, "temperature", UtilsNumber.GetNumberForLength(temperature, 2).ToString(), "温度", result);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

    }

}
